<?php

namespace App\Trip\Traits\Plane;

use App\Facades\Plane\PlaneCompanySeedFacade;
use App\Trip\Enums\Plane\PlaneTypes;
use App\Trip\Plane\CarrierCode;
use App\Trip\Plane\PlaneTypeName;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Exception;
use Illuminate\Support\Str;

trait PlaneQueryCommonTrait
{
    public function getArriveName($short = false): string
    {
        $f = $this->flightRepo->findOneByCode($this->getArriveCode());
        if ($short) {
            return $f?->getShortName() ?: '';
        }
        return $f?->getName() ?: '';
    }

    public function getArriveNameUy($short = false): string
    {
        $f = $this->flightRepo->findOneByCode($this->getArriveCode());
        if ($short) {
            return $f?->getShortNameUy() ?: '';
        }
        return $f?->getNameUy() ?: '';
    }

    public function getDepartNameUy($short = false): string
    {
        $f = $this->flightRepo->findOneByCode($this->getDepartCode());
        if ($short) {
            return $f?->getShortNameUy() ?: '';
        }
        return $f?->getNameUy() ?: '';
    }

    public function getDepartName($short = false): string
    {
        $f = $this->flightRepo->findOneByCode($this->getDepartCode());
        if ($short) {
            return $f?->getShortName() ?: '';
        }
        return $f?->getName() ?: '';
    }

    /**
     * 到达时间 时分
     * @throws InvalidFormatException
     */
    public function getArriveTime(): string
    {
        if (!$this->getArriveAt()) {
            return "";
        }
        return Carbon::parse($this->getArriveAt())->format('H:i');
    }

    /**
     * 出发时间 时分
     * @throws InvalidFormatException
     */
    public function getDepartTime(): string
    {
        return Carbon::parse($this->getDepartAt())->format('H:i');
    }

    /**
     * 里程时间
     * @return int
     * @throws InvalidFormatException
     */
    public function getRunTimes(): int
    {
        return Carbon::parse($this->getArriveAt())->diffInMinutes($this->getDepartAt());
    }

    public function getPlaneTypeNameUy(): string
    {
        $type_name_prefix = PlaneTypeName::getPlaneTypeName($this->getPlaneTypeName());
        $type             = PlaneTypes::tryFrom($type_name_prefix);
        if (!$type) {
            $name = $this->getPlaneTypeName();
        } else {
            $name = Str::replaceFirst($type_name_prefix, $type->getNameUy(), $this->getPlaneTypeName());
        }

        // 将planeSize 加括号换成维语
        $size = $this->getPlaneSize();
        if ($size && Str::contains($name, "($size)")) {
            $name = Str::replaceLast($size, $this->getPlaneSizeUy(), $name);
        }
        return $name;
    }

    public function getPlaneSizeUy(): string
    {
        return trans2uy($this->getPlaneSize());
    }

    /**
     * @throws Exception
     */
    public function getCarrierName($short = false): string
    {
        if ($short) {
            return PlaneCompanySeedFacade::getCompanyShortNameByCode($this->getCarrierCode());
        }
        return PlaneCompanySeedFacade::getCompanyNameByCode($this->getCarrierCode());
    }

    /**
     * @throws Exception
     */
    public function getCarrierCode(): string
    {
        return CarrierCode::getCarrierCode($this->getFlightNo());
    }

    /**
     * @throws Exception
     */
    public function getCarrierNameUy($short = false): string
    {

        if ($short) {
            return PlaneCompanySeedFacade::getCompanyShortNameUyByCode($this->getCarrierCode());
        }
        return PlaneCompanySeedFacade::getCompanyNameUyByCode($this->getCarrierCode());
    }

    /**
     * @throws Exception
     */
    public function getCabinNameUy(): string
    {
        return $this->getCabinName() ? trans2uy($this->getCabinName()) : "";
    }

    /**
     * 出发日期和到达日期 差值
     * @throws InvalidFormatException
     */
    function getDayDifference(): int
    {
        if (!$this->getArriveAt()) {
            return 0;
        }
        return Carbon::parse($this->getArriveDate())->diffInDays($this->getDepartDate());
    }

    /**
     * 到达日期
     * @throws InvalidFormatException
     */
    public function getArriveDate(): string
    {
        if (!$this->getArriveAt()) {
            return "";
        }
        return Carbon::parse($this->getArriveAt())->format('Y-m-d');
    }

    /**
     * 出发日期
     * @throws InvalidFormatException
     */
    public function getDepartDate(): string
    {
        return Carbon::parse($this->getDepartAt())->format('Y-m-d');
    }


    public function getMeal(): int
    {
        return $this->getMealType() ? 1 : 0;
    }
}